'use strict';

module.exports = app => {
    return class extends app.Controller {
        * index(ctx) {
            console.log(ctx.request);
            console.log("query",ctx.request.query);
            const query=ctx.request.query;
            const wechatId=ctx.request.header.wechatid;
            var pageParams;
            if(query.pageParams){
                pageParams =JSON.parse(ctx.request.query.pageParams);
                if(!pageParams.order){
                    pageParams.order=[['createdAt','DESC']];
                }
            }else {
                pageParams={
                    pageNum:1,
                    pageLimit:6,
                    pageTotal:0,
                    order:[['createdAt','DESC']]
                }
            }
            var offset=(pageParams.pageNum - 1) * pageParams.pageLimit;
            var items=yield ctx.model.query("SELECT score.*,user.`nickname`,article.`title` from `scores` as score left JOIN `users` as user ON `score`.`userId`=`user`.`id`  left join `articles` as article  on score.`articleId`=article.`id` where 1=1 and score.`deletedAt` is NULL and  user.`wechatId`="+wechatId+" ORDER BY comment.`updatedAt` DESC limit "+offset+","+pageParams.pageLimit, { type: ctx.model.QueryTypes.SELECT })
            var count=yield ctx.model.query("SELECT score.*,user.`nickname`,article.`title` from `scores` as score left JOIN `users` as user ON `score`.`userId`=`user`.`id`  left join `articles` as article  on score.`articleId`=article.`id` where 1=1 and score.`deletedAt` is NULL and  user.`wechatId`="+wechatId, { type: ctx.model.QueryTypes.SELECT });

            pageParams.pageTotal=Math.ceil(count/pageParams.pageLimit);
            yield ctx.body={
                data:items,
                pageParams:pageParams
            };
        }
        * destroy(ctx){
            let id=ctx.params.id;
            let result=yield ctx.model.Score.destroy({where:{id:id}});
            if(result>0){

                ctx.body={
                    message:"删除成功"
                }
            }else {
                ctx.status=405;
                ctx.body={
                    message:"删除请稍后"
                }
            }

        }
        * create(ctx){
            let body=ctx.request.body;
            body.userId=ctx.user.id;
            let data=yield ctx.model.ArticleCoScoremment.create({content:body.content,articleId:body.articleId,userId:ctx.user.id});
            yield ctx.model.Score.Score({userId:ctx.user.id,fraction:body.num,articleId:body.articleId});
            if(data){
                ctx.body={
                    message:"新增成功！"
                }
            }
        }
        * show(ctx){
            let id=ctx.params.id;
            let Score=yield ctx.model.Score.findById(id);
            article.view++;
            article.save();
            ctx.body=Score;
        }
        * update(ctx){
            let body=ctx.request.body;
            let data=yield ctx.model.Score.update(body,{where:{id:body.id}});
            if(data){
                ctx.body={
                    message:"修改成功！"
                }
            }
        }
    };
};
